package com.sense.kukoo.biz_me.model

import com.sense.kukoo.common.mvvm.model.BaseMvvmModel
import com.sense.kukoo.common.repo.bean.ResultBean
import com.sense.kukoo.common.repo.http.KukooHttpApi
import com.sense.kukoo.common.repo.http.KukooHttpManager
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

class ProfileModel:BaseMvvmModel() {

    private val TAG: String = javaClass.simpleName
    override fun onModelCleared() {}

    suspend fun uploadPic(file: File): ResultBean<String> {
        val params = HashMap<String, Any>()
        params.put("type", 1)
        val requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file) as RequestBody
        val part = MultipartBody.Part.createFormData( "file", file.name, requestFile ) as MultipartBody.Part
        return KukooHttpManager.instance.createHttpClient(KukooHttpApi::class.java).uploadPic(params, part)
    }

    suspend fun edit(
        avatarUrl: String?,
        nickname: String?,
    ): ResultBean<Boolean> {

        val hashMap = HashMap<String, Any>()
        if (avatarUrl!=null) {
            hashMap.put("avatarUrl", avatarUrl)
        }
        if (nickname!=null) {
            hashMap.put("channelName", nickname)
        }
        val mRequestBody: RequestBody = KukooHttpManager.createRequestBody(hashMap)

        return KukooHttpManager.instance.createHttpClient(KukooHttpApi::class.java).edit(mRequestBody)
    }
}